/*
 * @(#)XMenu.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.util;

import javax.swing.*;
import java.awt.event.*;

/**
 * Clase que desarrolla un menu con la estetica comun del entorno
 * 
 * @author Francisco Jose Moreno Velo
 *
 */
public class XMenu extends JMenu {

	//----------------------------------------------------------------------------//
	//                           CONSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603008L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Lista de elementos del men�
	 */
	private JMenuItem item[];

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Construye un menu con un titulo y una lista de elementos
	 */
	public XMenu(String name, String label[]) {
		super(name);
		int counter = 0;
		for(int i=0; i<label.length; i++) if(label[i] != null) counter++;

		item = new JMenuItem[counter];
		for(int i=0,j=0; i<label.length; i++) {
			if(label[i] == null) { addSeparator(); continue; }
			item[j] = new JMenuItem(label[i]);
			item[j].setEnabled(true);
			item[j].setFont(XConstants.font);
			add(item[j]);
			j++;
		}
		setFont(XConstants.font);
	}

	/**
	 * Construye el menu con titulo, elementos y acciones
	 */
	public XMenu(String name, String label[], ActionListener action[]) {
		this(name,label);
		for(int i=0; i<item.length; i++) item[i].addActionListener(action[i]);
	}

	/**
	 * Construye el menu con titulo, elementos, comandos y un controlador de acciones
	 */
	public XMenu(String name, String label[], String[] command, ActionListener action) {
		this(name, label);
		for (int i = 0; i < item.length; i++) {
			item[i].setActionCommand(command[i]);
			item[i].addActionListener(action);
		}
	}

	//----------------------------------------------------------------------------//
	//                             METODOS PUBLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * (Des)Habilita un elemento del menu
	 */
	public void setEnabled(boolean enable, int index) {
		if(index<0 || index>item.length) return;
		item[index].setEnabled(enable);
	}
}
